import requests

from apis.login import LoginApi

ipa = 'http://192.168.136.129:8080'


class Log:
    #初始即调用
    def __init__(self, t):
        self.token = t
        self.header = {'Admin-Token': self.token}

    # 文件上传
    url0 = f'{ipa}/file/upload'
    def file_upload(self):
        fdata = {'type': 'img'}

        ffile = {'file':
                     ('Capture001.png',
                      open(r'F:\Python\apiTest\datas\Capture001.png', 'rb'),
                      'image/png'
                      )}
        rr = requests.post(self.url0, data=fdata, files=ffile, headers=self.header)
        print(rr.json())
        return rr


    # 获取bid
    def get_bid(self):
        rr = self.file_upload()
        bid = rr.json()['batchId']
        return bid

    # 新增日志3
    url = f'{ipa}/OaLog/addOrUpdate'

    def addlog(self,bid):
        m1data = {
            'categoryId': '1',
            'content': '今日内容描述',
            'tomorrow': '明日内容描述',
            'question': '今日问题描述',
            'batchId':bid
        }


        r3 = requests.post(self.url, json=m1data, headers=self.header)
        print(r3.json())


#修改日志4
    url1=f'{ipa}/OaLog/addOrUpdate'
    def modifylog(self):
        m2data = {
            'content':'修改测试',
            'tomorrow':'测试修改',
            'question':'测修'
        }
        r4=requests.post(self.url1,json=m2data,headers=self.header)
        return r4




# 获取日志5
    url2=f'{ipa}/OaLog/queryList'
    def getlog(self):
        m3data = {
            'page':1,
            'limit':5,
            'by':'1'
        }
        r5=requests.post(self.url2,json=m3data,headers=self.header)
        return r5


#删除日志6
    url3=f'{ipa}/OaLog/deleteById'
    def deletelog(self,id):
        m4data = {
            'logId':id,
        }
        r6=requests.post(self.url3,data=m4data,headers=self.header)
        return r6
#循环删除日志
    def delAllogs(self):
        r=self.getlog()
        l=r.json()['data']['list']
        for i in l:
            self.deletelog(i['logId'])


if __name__ == '__main__':
    #实例化获取token赋给一个变量
    token = LoginApi().get_token()
    log = Log(token)
    # bid = log.get_bid()
    #新增日志
    # log.addlog(bid)
    #修改日志
    # log.modifylog()
    #删除所有日志
    # log.delAllogs()


